package com.blockmeta.bbs.baselibrary.net.interceptor;

import com.blankj.utilcode.util.LogUtils;

import com.blankj.utilcode.util.LogUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * @author AirThor
 * @date 2019/3/25.
 * 响应拦截器
 */
public class ResponseInterceptor implements Interceptor{
    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response = chain.proceed(chain.request());
        if (response.body() != null) {
            MediaType mediaType = response.body().contentType();
            if(mediaType != null) {
                String mediaTypeStr = mediaType.toString();
                if (mediaTypeStr.contains("json")
                        || mediaTypeStr.contains("text")
                        || mediaTypeStr.contains("message")) {
                    String string = response.body().string();
                    LogUtils.d("response" + "intercept: " + string);
                    ResponseBody responseBody = ResponseBody.create(mediaType, string);
                    return response.newBuilder().body(responseBody).build();
                }
            }
        }
        return response;
    }
}
